#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main()
{
    int n, p;
    cin >> n >> p;
    vector<int> temperature;
    temperature.reserve(n);
    for(int i = 0, a; i < n; i++) 
    {
        cin >> a;
        temperature.push_back(a);
    }
    sort(temperature.begin(), temperature.end());
    int restrict = temperature[n - 1];
    int ans = 0;
    for(int left = 0, right = 0, cur = 0; right < n && cur <= restrict; cur++)
    {
        int left_limit = cur - p, right_limit = cur + p;
        //入窗口
        while(right < n && temperature[right] <= right_limit)
            right++;
        //出窗口
        while(left <= right && temperature[left] < left_limit)
            left++;
        ans = max(ans, right - left);
    }
    cout << ans << endl;
    return 0;
}